package cn.ctvonline.android.kaopu.fragment.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;

import java.util.List;

import cn.ctvonline.android.kaopu.R;
import cn.ctvonline.android.kaopu.base.util.SimpleUtil;
import cn.ctvonline.android.kaopu.entity.AmapSearchResult;
import cn.ctvonline.android.kaopu.fragment.FeatureFragment.OnListFragmentInteractionListener;

/**
 * {@link RecyclerView.Adapter} that can display a {@link AmapSearchResult} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class FeatureRecyclerViewAdapter extends RecyclerView.Adapter<FeatureRecyclerViewAdapter.ViewHolder> {
    static final String TAG = FeatureRecyclerViewAdapter.class + "";
    static final int VIEW_TYPE_HEADER = 0x01;
    static final int VIEW_TYPE_NORMAL = 0x02;
    private boolean useHeadView;
    private ViewGroup headView;
    private final List<PoiItem> mValues;
    private final OnListFragmentInteractionListener mListener;

    public FeatureRecyclerViewAdapter(List<PoiItem> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i(TAG, "onCreateViewHolder viewType = " + viewType);
        ViewHolder vh = null;
        if (viewType == VIEW_TYPE_NORMAL) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.feature_item, parent, false);
            vh = new ViewHolder(view);
        } else if (viewType == VIEW_TYPE_HEADER) {
            vh = new ViewHolder(headView);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder getItemViewType = " + getItemViewType(position) + ", position=" + position);
        if (getItemViewType(position) == VIEW_TYPE_NORMAL
                && position <= mValues.size()) {
            position = useHeadView ? position - 1 : position;
            PoiItem item = mValues.get(position);
            holder.mResult = item;
            holder.mIdView.setText("店名:" + item.getTitle());
            int distance = item.getDistance();
            double almostDis = ((double) distance) / 1000;
            String distanceLabel = SimpleUtil.formatNum(almostDis);
            holder.mContentView.setText("地址:" + item.getSnippet() + "\n距离:约 " + distanceLabel + "公里");
            holder.mView.setTag(position);
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mListener) {
                        mListener.onListFragmentInteraction(holder.mResult, v);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        int viewType;
        if (useHeadView && position == 0) {
            viewType = VIEW_TYPE_HEADER;
        } else {
            viewType = VIEW_TYPE_NORMAL;
        }
        return viewType;
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (useHeadView) {
            count += 1;
        }
        count += mValues.size();
        return count;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public View mView;
        public ImageView mAddIV;
        public TextView mIdView;
        public TextView mContentView;
        public PoiItem mResult;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mAddIV = (ImageView) view.findViewById(R.id.feature_ad_iv);
            mIdView = (TextView) view.findViewById(R.id.feature_ad_intro_tv);
            mContentView = (TextView) view.findViewById(R.id.feature_ad_detail_tv);
        }

        public ViewHolder(ViewGroup viewGroup) {
            super(viewGroup.getRootView());
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }

    public ViewGroup getHeadView() {
        return headView;
    }

    public void setHeadView(ViewGroup headView) {
        this.headView = headView;
        if (headView != null) {
            useHeadView = true;
        }
    }
}
